<?php
	include "./include/om_config.inc";

  $smarty = new SmartyWWW();
  $fromDate = isset($_POST['fromDateYear'])? $_POST['fromDateYear']."-".strtoupper($_POST['fromDateMonth'])."-".$_POST['fromDateDay']:mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
  $toDate = isset($_POST['toDateYear'])? $_POST['toDateYear']."-".strtoupper($_POST['toDateMonth'])."-".$_POST['toDateDay']:mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
  $smarty->assign("fromDate",$fromDate);
  $smarty->assign("toDate",$toDate);
  
  $displayItemName =0;
  if(isset($_POST['itemId']) && $_POST['itemId'] == '0')
  {
  	$displayItemName = '1';
  }
  $smarty->assign("displayItemName",$displayItemName);
  /////For Display in table ////
  
         
   $weekDay          = array();
   $expiryDateBc     = array();
   $itemName         = array();
   $bhavcopyDate     = array();
   $openPrice        = array();
   $highPrice        = array();
   $lowPrice         = array();
   $closePrice       = array();
   $atp              = array();
   $exchangeValue    = array();
   $highestHighPrice = 0 ;
   $lowestLowPrice   = 9999;

   $selectQuery = "SELECT exchange,contractCode,expiryDate from bhavcopy ORDER BY bhavcopyDate,expiryDate";
   $resultQuery = mysql_query($selectQuery);
	 $count=0;
   while($rowQuery = mysql_fetch_array($resultQuery))
   {
   	 $contract[$count] = $rowQuery['contractCode'];
   	 $expiry[$count]   = $rowQuery['expiryDate'];
		 $exchange[$count] = $rowQuery['exchange'];
		 $count++;
	 }
   $currentItemId   =isset($_POST['itemId'])? $_POST['itemId'] :0;
   $expDate         = isset($_POST['expiryDate'])? $_POST['expiryDate'] : $expiry[0]; 
   $currentExchange = isset($_POST['exchange'])? $_POST['exchange'] : $expiry[0]; 
   
   $displayCount = 0;  
   $displayQuery = "SELECT * FROM bhavcopy WHERE bhavcopyDate >= '".$fromDate."' AND bhavcopyDate <= '".$toDate."'";
   if($currentItemId > '0')									
   {
   	 $displayQuery .= " AND contractCode='".$currentItemId."'";
   
	   if($currentExchange !='BSE CASH')
	   { 
	     $displayQuery .= "AND expiryDate='".$expDate."'"; 
	   }
   }
   $displayQuery .="  AND exchange='".$currentExchange."' ORDER BY bhavcopyDate,contractCode,expiryDate";
   $displayResult = mysql_query($displayQuery);
   while($displayRow = mysql_fetch_array($displayResult))
   {   
   		$weekDay[$displayCount]      = date("l", mktime(0,0,0,substr($displayRow['bhavcopyDate'],5,2),substr($displayRow['bhavcopyDate'],8,2),substr($displayRow['bhavcopyDate'],0,4)));
   		$bhavcopyDate[$displayCount] = mysqlToDDMMYY($displayRow['bhavcopyDate']);
      $itemName[$displayCount] 		 = $displayRow['contractCode'];
      $expiryDateBc[$displayCount] = $displayRow['expiryDateBc'];
      $openPrice[$displayCount] 	 = $displayRow['openPrice'];
      $highPrice[$displayCount]  	 = $displayRow['highPrice'];
      $lowPrice[$displayCount] 		 = $displayRow['lowPrice'];
      $closePrice[$displayCount]   = $displayRow['closePrice'];
      $atp[$displayCount]  			   = $displayRow['atp'];
      
      //To find the Highest Price and Lowest Price form data : Starts
      if($highestHighPrice < $highPrice[$displayCount])
      {
        $highestHighPrice = $highPrice[$displayCount];
      }
      if($lowestLowPrice > $lowPrice[$displayCount] && $lowPrice[$displayCount] != 0)
      {
        $lowestLowPrice = $lowPrice[$displayCount];
      }
      //To find the Highest Price and Lowest Price form data : Ends
      $displayCount++;
   }
  
  ////Request parameters, if passed : transfer to proper variable :End
  
  //Item records :Start
  $itemIdSelected = $currentItemId;
  $itemIdValues = array();
  $itemIdOptions = array();
  $itemCount = 0;
  $itemIdValues[0]  = '0';
  $itemIdOptions[0] = "All";
  $itemRecords = array();
  $itemCount++;
  $selectedExchange = isset($_POST['exchange'])? $_POST['exchange'] :'MCX';
  $smarty->assign("selectedExchange",$selectedExchange);
  
  $itemQuery = "SELECT DISTINCT(contractCode) FROM bhavcopy where exchange='".$selectedExchange."' ORDER BY contractCode";
  $itemResult = mysql_query($itemQuery);
  while($itemRow = mysql_fetch_array($itemResult))
  {
    $itemIdValues[$itemCount]  = $itemRow['contractCode'];
    $itemIdOptions[$itemCount] = $itemRow['contractCode'];
    $itemCount++;
  }
  
  //Expiry records :Start
  if(isset($_POST['expiryDate']))
  {
    if($_POST['itemIdChanged']==1 || $currentItemId=="All")
      $expiryDateSelected = 0;
    else
      $expiryDateSelected = $_POST['expiryDate'];
  }
  else
    $expiryDateSelected = 0;
    
  $expiryDateValues = array();
  $expiryDateOptions = array();
  
  $expiryDateValues[0]  = 0;
  $expiryDateOptions[0] = 'All';
  $i=0;
  
  
  if($currentItemId!="All")
  {
    $expiryQuery = "SELECT DISTINCT(expiryDate),expiryDateBc FROM bhavcopy WHERE  contractCode='".$currentItemId."'ORDER BY expiryDate";
    $expiryResult = mysql_query($expiryQuery);
    while($expiryRow = mysql_fetch_array($expiryResult))
    {
        $expiryDateValues[$i]  = $expiryRow['expiryDate'];
        $expiryDateOptions[$i] = $expiryRow['expiryDateBc'];
        $i++;
    }
  }
  if(isset($_POST['avgLow']) && isset($_POST['avgHigh']))
  {	
    $avgHigh=$_POST['avgHigh'];
    $avgLow=$_POST['avgLow'];
  }
  
  $selectExchange="SELECT DISTINCT (exchange) FROM bhavcopy";
  $resultExchange = mysql_query($selectExchange);    
  $j=0;
  while($rowExchange = mysql_fetch_array($resultExchange))
  {
  	$exchangeValue[$j] = $rowExchange['exchange'];
  	$j++;
  }
   
  //////End for display in table //////////
  
  

  $avgHigh = isset($avgHigh)? $avgHigh : "";

  $smarty->assign("weekDay",$weekDay);
  $smarty->assign("itemName",$itemName);
  $smarty->assign("expiryDateBc",$expiryDateBc);
  $smarty->assign("bhavcopyDate",$bhavcopyDate);
  $smarty->assign("openPrice",$openPrice);
  $smarty->assign("highPrice",$highPrice);
  $smarty->assign("lowPrice",$lowPrice);
  $smarty->assign("closePrice",$closePrice);
  $smarty->assign("atp",$atp);
  
  $smarty->assign("itemIdSelected",$itemIdSelected);
  $smarty->assign("itemIdValues",$itemIdValues);
  $smarty->assign("itemIdOptions",$itemIdOptions);
  $smarty->assign("expiryDateSelected",$expiryDateSelected);
  $smarty->assign("expiryDateValues",$expiryDateValues);
  $smarty->assign("expiryDateOptions",$expiryDateOptions);
  $smarty->assign("exchangeValue",$exchangeValue);
  $smarty->assign("highestHighPrice",$highestHighPrice);
  $smarty->assign("lowestLowPrice",$lowestLowPrice);
  
  $smarty->display('bhavCopyList.tpl');
  ?>